package com.aixin.project.servlet.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import com.aixin.project.data.User;
import com.aixin.project.handler.UserHandler;
import com.aixin.project.util.Pagenation2;
import com.aixin.project.util.RequestUtil;

public class SchoolAdminServlet extends AdminServlet{

	@Override
	protected Template execute(HttpServletRequest request, HttpServletResponse response, Context ctx) {
		String method = request.getParameter("method");
		if("pwd".equals(method)){
			String ppwd = request.getParameter("ppwd");
			String password = RequestUtil.toMD5(request.getParameter("pwd1"));
			UserHandler.updatepwd(ppwd,password);
		}else if("status".equals(method)){
			String schid= request.getParameter("schid");
			String status = request.getParameter("status"+schid);
			UserHandler.update(schid, status);
		}
		long dsh = UserHandler.find();
		request.setAttribute("dsh", dsh);
		
		//pagination
		int status = RequestUtil.getInt(request,"s",-1);
        int page = RequestUtil.getInt(request,"p",1);
        StringBuffer params = new StringBuffer();
        if(page!=1)
            params.append("&p=").append(page);
        if(status!=-1)
            params.append("&s=").append(params);
        if(params.length()>0)
            ctx.put("params",params.substring(1,params.length()));
        int psize = 10;
        List<User> list = UserHandler.getAdminUsers(page,psize,status);
        int count = (int)UserHandler.count(status);
        Pagenation2 pg = new Pagenation2(page,psize,count,list,"/admin/sch",params.toString());
        ctx.put("pg",pg);
        
        ctx.put("s", status);
        ctx.put("users", list);
        ctx.put("navschool", true);
		return getTemplate("bg/school.vm");
	}

}
